var request = require('request');
var req = {};

req.getData = function(url, cb) {
    request.get({
        url: url,
    }, function(err, httpResponse, body) {
        if (!err) {
            var result = JSON.parse(body);
            if (result.code === '0') {
                cb(null, result.data);
            } else {
                cb(result.code, result.msg);
            }
        } else {
            console.log(err);
            cb('-1');
        }
    });
}

req.postData = function(url, obj, cb) {
    request.post({
        url: url,
        json: obj
    }, function(err, httpResponse, body) {
        if (!err) {
            var result = body;
            if (result.code === '0') {
                cb(null, result.data);
            } else {
                cb(result.code, result.msg);
                console.log(result.msg);
            }
        } else {
            console.log(err);
            cb('-1','与服务器连接异常， 请稍后重试！');
        }
    });
}

module.exports = req;